// pages/photo/photo.js
const config = require('../../config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageIndex: 1,
        id: '',
        baseUrl: config.basePath,
        fullLoading: true,
        photoLoding: true,
        photoData: [],
        total: 0,
        password: '',
        imgList: [],
        name: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        wx.setNavigationBarTitle({
            title: options.name
        });
        that.setData({
            id: options.id,
            password: options.password,
            name: options.name
        });
        this.getPhotoList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.setData({
            fullLoading: false
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if (that.data.total <= that.data.photoData.length) {
            return;
        }
        that.setData({
            pageIndex: that.data.pageIndex + 1
        });
        that.getPhotoList();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    onShareTimeline(res) {
        if (this.data.password != "" && this.data.password != " ") {
            return {
                title: config.miniName + "-相册",
                path: "/pages/photos/photos"
            }
        } else {
            return {
                title: config.miniName + "-" + this.data.name,
                path: "/pages/photo/photo?id=" + this.data.id + '&name=' + this.data.name + '&password=',
                imageUrl: (this.data.imgList.length > 0) ? this.data.imgList[0] : ""
            }
        }
    },
    getPhotoList: function () {
        let that = this;
        wx.request({
            url: config.basePath + '/mini/photo/list',
            method: 'GET',
            data: {
                pageIndex: that.data.pageIndex,
                pageSize: 5,
                id: that.data.id,
                password: that.data.password
            },
            success(res) {
                if (res.data.code === 200) {
                    that.setData({
                        photoData: that.data.photoData.concat(res.data.data),
                        total: res.data.total,
                        photosLoding: true
                    });

                    if (that.data.total <= that.data.photoData.length) {
                        that.setData({
                            photoLoding: false
                        });
                    }

                    that.data.photoData.forEach(function (item, index) {
                        let img = [that.data.baseUrl + '/static-plugin/photos' + item.url];
                        that.setData({
                            imgList: that.data.imgList.concat(img)
                        });
                    });
                } else {
                    wx.lin.showMessage({
                        type: 'error',
                        content: '服务出错'
                    })
                }
            },
            fail(err) {
                that.setData({
                    photoLoding: false
                });
                wx.lin.showMessage({
                    type: 'error',
                    content: '网络出错'
                })
            }
        });
    },
    viewImg: function (event) {
        let that = this;
        wx.previewImage({
            current: event.currentTarget.dataset.src,
            urls: that.data.imgList
        })
    }
})